package com.dhcc.sds.base.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.result.node.ZTreeNode;
import com.dhcc.sds.base.dao.SdsIndexFieldMapper;
import com.dhcc.sds.base.dao.SdsSecondaryHeaderMapper;
import com.dhcc.sds.base.entity.SdsIndexFieldEntity;
import com.dhcc.sds.base.entity.SdsQcfitemDicEntity;
import com.dhcc.sds.base.entity.SdsQcformItemEntity;
import com.dhcc.sds.base.entity.SdsSecondaryHeaderEntity;
import com.dhcc.sds.base.service.ISdsQceitemDicService;
import com.dhcc.sds.base.service.ISdsQcfitemDicService;
import com.dhcc.sds.base.service.ISdsQcformItemService;
import com.dhcc.sds.base.service.ISdsSecondaryHeaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 单病种指标统计配置二级表头服务层接口
 *
 * @ClassName: SdsIndConfigService
 * @Description: TODO
 * @author zhangdc
 * @date 2021-08-24 11:14:53
 */
@Service("sdsSecondaryHeaderService")
public class SdsSecondaryHeaderServiceImpl extends BaseServiceImpl<SdsSecondaryHeaderMapper, SdsSecondaryHeaderEntity> implements ISdsSecondaryHeaderService {

    @Resource
    private SdsIndexFieldMapper sdsIndexFieldMapper;
    @Autowired
    private ISdsQcformItemService sdsQcformItemService;
    @Autowired
    private ISdsQcfitemDicService sdsQcfitemDicService;

    @Override
    public List<SdsSecondaryHeaderEntity> page(Page<SdsSecondaryHeaderEntity> page, Map<String, Object> map) {
        return this.baseMapper.page(page, map);
    }

    @Override
    public List<SdsSecondaryHeaderEntity> list(Map<String, Object> map) {
        return this.baseMapper.list(map);
    }

    @Override
    public void compare(Long fieldId, Long formItemId, Long formDicId) {
        SdsSecondaryHeaderEntity sdsSecondaryHeaderEntity = this.baseMapper.selectById(fieldId);
        SdsIndexFieldEntity sdsIndexFieldEntity = new SdsIndexFieldEntity();
        if (!StringUtils.isEmpty(sdsSecondaryHeaderEntity)){
            /*if (!StringUtils.isEmpty(formItemId)){
                //病种项目关联
                sdsIndexFieldEntity.setSecondaryHeaderId(fieldId);
                sdsIndexFieldEntity.setFormItemId(formItemId);
            }
            if (!StringUtils.isEmpty(formDicId)){
                //病种值域关联
                sdsIndexFieldEntity.setSecondaryHeaderId(fieldId);
                sdsIndexFieldEntity.setFormDicId(formDicId);
            }*/
            sdsIndexFieldEntity.insert();
        }
    }

    @Override
    public void undo(Long fieldId, Long formItemId, Long formDicId) {
        SdsIndexFieldEntity sdsIndexFieldEntity = new SdsIndexFieldEntity();
        /*sdsIndexFieldEntity.setSecondaryHeaderId(fieldId);
        if (!StringUtils.isEmpty(formItemId)){
            sdsIndexFieldEntity.setFormItemId(formItemId);
        }
        if (!StringUtils.isEmpty(formDicId)){
            sdsIndexFieldEntity.setFormDicId(formDicId);
        }*/
        SdsIndexFieldEntity sdsIndexFieldEntity1 = sdsIndexFieldMapper.selectOne(sdsIndexFieldEntity);
        sdsIndexFieldMapper.deleteById(sdsIndexFieldEntity1);
    }

    @Override
    public List<ZTreeNode> getFieldTree() {
        List<ZTreeNode> tree = this.baseMapper.tree();
        return tree;
    }

    @Override
    public List<SdsQcformItemEntity> pageFormItem(Page<SdsQcformItemEntity> page, Map<String, Object> params) {
        Object fieldId = params.get("fieldId");
        Map<String, Object> map = new HashMap<>();
        map.put("secondaryHeaderId", Long.valueOf(String.valueOf(fieldId)));
        List<SdsIndexFieldEntity> list = sdsIndexFieldMapper.list(map);
        List<SdsQcformItemEntity> page1 = sdsQcformItemService.page(page, params);
        //关联的数据进行勾选
        for (SdsIndexFieldEntity sdsIndexFieldEntity : list){
            for (SdsQcformItemEntity sdsQcformItemEntity : page1){
                /*if (Objects.equals(sdsIndexFieldEntity.getFormItemId(), sdsQcformItemEntity.getId())){
                    sdsQcformItemEntity.setIsCheck(true);
                }*/
            }
        }
        //已对照-yes，未对照-no，全部
        Object o = params.get("compare");
        String compare = String.valueOf(o);
        List<SdsQcformItemEntity> collect = new ArrayList<>();
        if (Objects.equals("yes", compare)){
            collect = page1.stream().filter(item -> (item.getIsCheck() != null && item.getIsCheck() == true)).collect(Collectors.toList());
            return collect;
        }else if (Objects.equals("no", compare)){
            collect = page1.stream().filter(item -> (item.getIsCheck() == null || "".equals(item.getIsCheck()))).collect(Collectors.toList());
            return collect;
        }else {
            return page1;
        }
    }

    @Override
    public List<SdsQcfitemDicEntity> pageFormDic(Page<SdsQcfitemDicEntity> page, Map<String, Object> params) {
        Object fieldId = params.get("fieldId");
        Map<String, Object> map = new HashMap<>();
        map.put("secondaryHeaderId", Long.valueOf(String.valueOf(fieldId)));
        List<SdsIndexFieldEntity> list = sdsIndexFieldMapper.list(map);
        List<SdsQcfitemDicEntity> page1 = sdsQcfitemDicService.page(page, params);
        //关联的数据进行勾选
        for (SdsIndexFieldEntity sdsIndexFieldEntity : list){
            for (SdsQcfitemDicEntity sdsQcfitemDicEntity : page1){
                /*if (Objects.equals(sdsIndexFieldEntity.getFormDicId(), sdsQcfitemDicEntity.getId())){
                    sdsQcfitemDicEntity.setIsCheck(true);
                }*/
            }
        }
        //已对照-yes，未对照-no，全部
        Object o = params.get("compare");
        String compare = String.valueOf(o);
        List<SdsQcfitemDicEntity> collect = new ArrayList<>();
        if (Objects.equals("yes", compare)){
            collect = page1.stream().filter(item -> (item.getIsCheck() != null && item.getIsCheck() == true)).collect(Collectors.toList());
            return collect;
        }else if (Objects.equals("no", compare)){
            collect = page1.stream().filter(item -> (item.getIsCheck() == null || "".equals(item.getIsCheck()))).collect(Collectors.toList());
            return collect;
        }else {
            return page1;
        }
    }
}
